/**
 * 好友事务预约网站主要JavaScript文件
 */

// 当文档加载完成后执行
$(document).ready(function() {
    // 启用Bootstrap提示工具
    var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
    var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl);
    });
    
    // 自动隐藏警告消息
    // setTimeout(function() {
    //     $('.alert').alert('close');
    // }, 5000);
    
    // 表单验证
    $('.needs-validation').submit(function(event) {
        if (this.checkValidity() === false) {
            event.preventDefault();
            event.stopPropagation();
        }
        $(this).addClass('was-validated');
    });
    
    // 头像上传预览
    $('#avatarUpload').change(function() {
        var file = this.files[0];
        if (file) {
            var reader = new FileReader();
            reader.onload = function(e) {
                $('#avatarPreview').attr('src', e.target.result);
            }
            reader.readAsDataURL(file);
        }
    });
    
    // 密码强度检查
    $('#password').keyup(function() {
        var password = $(this).val();
        var strength = 0;
        
        if (password.length >= 6) strength += 1;
        if (password.match(/[a-z]+/)) strength += 1;
        if (password.match(/[A-Z]+/)) strength += 1;
        if (password.match(/[0-9]+/)) strength += 1;
        if (password.match(/[^a-zA-Z0-9]+/)) strength += 1;
        
        var strengthBar = $('#passwordStrength');
        switch (strength) {
            case 0:
            case 1:
                strengthBar.attr('class', 'progress-bar bg-danger').css('width', '20%').text('非常弱');
                break;
            case 2:
                strengthBar.attr('class', 'progress-bar bg-warning').css('width', '40%').text('弱');
                break;
            case 3:
                strengthBar.attr('class', 'progress-bar bg-info').css('width', '60%').text('中等');
                break;
            case 4:
                strengthBar.attr('class', 'progress-bar bg-primary').css('width', '80%').text('强');
                break;
            case 5:
                strengthBar.attr('class', 'progress-bar bg-success').css('width', '100%').text('非常强');
                break;
        }
    });
    
    // 确认密码验证
    $('#confirmPassword').keyup(function() {
        var password = $('#password').val();
        var confirmPassword = $(this).val();
        
        if (password === confirmPassword) {
            $('#passwordMatch').html('<span class="text-success">密码匹配</span>');
        } else {
            $('#passwordMatch').html('<span class="text-danger">密码不匹配</span>');
        }
    });
    
    // 搜索用户功能
    $('#searchUser').keyup(function() {
        var keyword = $(this).val();
        if (keyword.length >= 2) {
            $.ajax({
                url: '/api/user/search.php',
                type: 'GET',
                data: { keyword: keyword },
                success: function(response) {
                    $('#searchResults').html(response);
                    $('#searchResults').show();
                }
            });
        } else {
            $('#searchResults').hide();
        }
    });
    
    // 事务截止时间选择器
    if ($('#taskDeadline').length) {
        var now = new Date();
        var minDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes());
        $('#taskDeadline').attr('min', minDate.toISOString().slice(0, 16));
    }
});
